﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace SharePointROM
{
    /// <summary>
    /// Represents a SharePoint web object
    /// </summary>
    public class SPWeb : IDisposable
    {
        private SPSite _site;
        private Microsoft.SharePoint.WebServices.UserGroup.UserGroupSoapClient _webserviceUserGroup;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="site">SharePoint site object used to determine how to connect to site</param>
        public SPWeb(SPSite site)
        {
            _site = site;
        }

        /// <summary>
        /// Gets a list of users associated to the SharePoint web application
        /// </summary>
        /// <returns></returns>
        public ReadOnlyCollection<SPUser> GetUsers
        {
            get
            {
                var responseXml = UserGroupService.GetAllUserCollectionFromWeb();
                return null;
            }
        }

        private Microsoft.SharePoint.WebServices.UserGroup.UserGroupSoapClient UserGroupService
        {
            get
            {
                if (_webserviceUserGroup == null)
                {
                    _webserviceUserGroup = new Microsoft.SharePoint.WebServices.UserGroup.UserGroupSoapClient();
                    var uri = new Uri(_site.Url, "/_vti_bin/usergroup.asmx");
                    _webserviceUserGroup.Endpoint.Address = new System.ServiceModel.EndpointAddress(uri.ToString());
                    _webserviceUserGroup.ClientCredentials.HttpDigest.ClientCredential.UserName = _site.Username;
                    _webserviceUserGroup.ClientCredentials.HttpDigest.ClientCredential.Password = _site.Password;

                }
                return _webserviceUserGroup;
            }
        }


        #region IDisposable Members

        /// <summary>
        /// Disposes object
        /// </summary>
        public void Dispose()
        {
            if (_webserviceUserGroup != null)
            {
                _webserviceUserGroup.Close();
            }
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
